#include<stdio.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#include<arpa/inet.h>
#include<sys/select.h>
#include<mysql.h>
#include<string.h>
#include <pthread.h>

pthread_t tida,tidb;
int connectfd;

struct password
{
	char id[20];
	char passwd[20];
	int flag;
};


struct password pw;
char buf[100];

void *thread_function_write(void *arg)
{
    while(1)
    {
        memset(buf,0,sizeof(buf));
        recv(connectfd,buf,sizeof(buf),0);
        printf("接受消息：%s\n",buf);
    }
}

void *thread_function_send(void *arg)
{
    while(1)
    {
        memset(pw.passwd,0,sizeof(pw));
        printf("请输入您要传输的数据。\n");
        scanf("%s",pw.passwd);
        pw.flag=3;
        send(connectfd,&pw,sizeof(pw),0);
	if(strcmp(pw.passwd,"quit")==0)
	{
		return;
	}
    }
}


void private_chat(int fd)
{
	connectfd=fd;
	pw.flag=3;
	printf("输入你想联系的用户名:");
	scanf("%s",pw.id);
	send(connectfd,&pw,sizeof(pw),0);

        if(pthread_create(&tida,NULL,thread_function_write,NULL) != 0)
        {
            perror("pthread_create");
        }
        if(pthread_create(&tidb,NULL,thread_function_send,NULL) != 0)
        {
            perror("pthread_create");
        }
        pthread_join(tida, NULL);
        pthread_join(tidb, NULL);

}

